package Niveles 
{
	import Entidades.*;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class Nivel3 extends Sprite 
	{
		public var termino_nivel:Boolean = false;
		
		private var jugador:Personaje = new Personaje();
		private var se_mueve:Boolean = true;
		
		private var event_detec_1:Sprite = new Sprite();
		private var event_detec_2:Sprite = new Sprite();
		private var event_detec_3:Sprite = new Sprite();
		private var ocurrio_event1:Boolean;
		private var ocurrio_event2:Boolean;
		private var ocurrio_event3:Boolean;
		private var en_evento:Boolean = false;
		
		private var teleport_entra_1:Sprite = new Sprite();
		private var teleport_sale_1:Sprite = new Sprite();
		
		private var fin_nivel:Sprite = new Sprite();
		
		private var ev1boton1:Boton = new Boton();
		private var ev1boton2:Boton = new Boton();
		private var ev1boton3:Boton = new Boton();
		private var ev2boton1:Boton = new Boton();
		private var ev2boton2:Boton = new Boton();
		private var ev2boton3:Boton = new Boton();	
		private var ev3boton1:Boton = new Boton();
		private var ev3boton2:Boton = new Boton();
		private var ev3boton3:Boton = new Boton();
		
		private var contenedor_opciones:Sprite = new Sprite();
		
		public function Nivel3():void {
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event):void {
			
			set_context();
			addChild(jugador);
			jugador.buttonMode = true;
			jugador.useHandCursor = true;
			
			event_detec_1.graphics.beginFill(0xE9CE83);
			event_detec_1.graphics.drawRect(0, 0, 10, 50);
			event_detec_1.graphics.endFill();
			addChild(event_detec_1);
			
			event_detec_2.graphics.beginFill(0xE9CE83);
			event_detec_2.graphics.drawRect(0, 0, 10, 50);
			event_detec_2.graphics.endFill();
			addChild(event_detec_2);
			
			event_detec_3.graphics.beginFill(0xE9CE83);
			event_detec_3.graphics.drawRect(0, 0, 10, 50);
			event_detec_3.graphics.endFill();
			addChild(event_detec_3);
			
			contenedor_opciones.graphics.beginFill(0xE5B85D);
			contenedor_opciones.graphics.drawRect(0, stage.stageHeight * 7 / 8, stage.stageWidth, stage.stageHeight / 8)
			contenedor_opciones.graphics.endFill();
			contenedor_opciones.visible = false;
			addChild(contenedor_opciones);
			
			teleport_entra_1.graphics.beginFill(0xFF3333);
			teleport_entra_1.graphics.drawCircle(0, 0, 5);
			teleport_entra_1.graphics.endFill();
			addChild(teleport_entra_1);
			
			teleport_sale_1.graphics.beginFill(0xFF3333);
			teleport_sale_1.graphics.drawCircle(0, 0, 5);
			teleport_sale_1.graphics.endFill();
			addChild(teleport_sale_1);
			
			fin_nivel.graphics.beginFill(0x0000CC);
			fin_nivel.graphics.drawCircle(0, 0, 5);
			fin_nivel.graphics.endFill();
			addChild(fin_nivel)
			
			event_detec_1.x = 350;
			event_detec_1.y = 125; 
			event_detec_2.x = 550;
			event_detec_2.y = 125;
			event_detec_3.x = 450;
			event_detec_3.y = 425;
			teleport_entra_1.x = 750;
			teleport_entra_1.y = 150;
			teleport_sale_1.x = 150;
			teleport_sale_1.y = 450;
			fin_nivel.x = 750;
			fin_nivel.y = 450;
			
			
			removeEventListener(Event.ADDED_TO_STAGE, init);
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			jugador.addEventListener(MouseEvent.CLICK, jugadorClick);
			
		}
		private function set_context():void {
			
			jugador.x = 150;
			jugador.y = 150;
			jugador.velocidad = 2;
			se_mueve = true;
			
			ocurrio_event1 = false;
			ocurrio_event2 = false;
			ocurrio_event3 = false;
		}
		
		private function jugadorClick(e:MouseEvent):void {
			if (!en_evento) {
				if (se_mueve) { se_mueve = false }
				else { se_mueve = true }
			}
		}
		
		private function onEnterFrame(e:Event):void {
			
			if (jugador.vivo){
				if (se_mueve) { jugador.x += jugador.velocidad };
				if (!ocurrio_event1 && jugador.hitTestObject(event_detec_1)) {
					se_mueve = false; event1(); en_evento = true;
					}
				if (!ocurrio_event2 && jugador.hitTestObject(event_detec_2)) {
					se_mueve = false; event2(); en_evento = true;
					}
				if (!ocurrio_event3 && jugador.hitTestObject(event_detec_3)) {
					se_mueve = false; event3(); en_evento = true;
					}
				if (jugador.hitTestObject(teleport_entra_1)) {
					teleport1();
					}
				if (jugador.hitTestObject(fin_nivel)) {
					se_mueve = false ; en_evento = true;
					termino_nivel = true;
					}
			}
			else {set_context(),jugador.vivo=true}
			
		}
		
		private function event1():void {
			
			ev1boton1.x = (stage.stageWidth / 3 - ev1boton1.width) / 2;
			ev1boton1.y = stage.stageHeight * 7 / 8 + (stage.stageHeight/8 - ev1boton1.height) / 2;
			addChild(ev1boton1);
			ev1boton1.buttonMode = true;
			ev1boton1.useHandCursor = true;
			ev1boton2.x = stage.stageWidth / 3 + (stage.stageWidth / 3 - ev1boton2.width) / 2;
			ev1boton2.y = stage.stageHeight * 7 / 8 + (stage.stageHeight/8 - ev1boton2.height) / 2;
			addChild(ev1boton2);
			ev1boton2.buttonMode = true;
			ev1boton2.useHandCursor = true;
			ev1boton3.x = stage.stageWidth * 2 / 3 + (stage.stageWidth / 3 - ev1boton3.width) / 2;
			ev1boton3.y = stage.stageHeight * 7 / 8 + (stage.stageHeight/8 - ev1boton3.height) / 2;
			addChild(ev1boton3);
			ev1boton3.buttonMode = true;
			ev1boton3.useHandCursor = true;
			
			ev1boton1.addEventListener(MouseEvent.CLICK, onClick1);
			ev1boton2.addEventListener(MouseEvent.CLICK, onClick1);
			ev1boton3.addEventListener(MouseEvent.CLICK, onClick1);
			contenedor_opciones.visible = true;
			
		}
		private function onClick1(e:MouseEvent):void {
			switch(e.currentTarget) {
				case ev1boton1:
					se_mueve = true;
					break;
				case ev1boton2:
					jugador.vivo = false;
					break;
				case ev1boton3:
					jugador.vivo = false;
					break;
			}
			removeEventListener(MouseEvent.CLICK, onClick1);
			removeChild(ev1boton1);
			removeChild(ev1boton2);
			removeChild(ev1boton3);
			ocurrio_event1 = true;
			en_evento = false;
			contenedor_opciones.visible = false;
			
		}
		
		private function event2():void {
			
			ev2boton1.x = (stage.stageWidth / 3 - ev2boton1.width) / 2;
			ev2boton1.y = stage.stageHeight * 7 / 8 + (stage.stageHeight/8 - ev2boton1.height) / 2;
			addChild(ev2boton1);
			ev2boton1.buttonMode = true;
			ev2boton1.useHandCursor = true;
			ev2boton2.x = stage.stageWidth / 3 + (stage.stageWidth / 3 - ev2boton2.width) / 2;
			ev2boton2.y = stage.stageHeight * 7 / 8 + (stage.stageHeight/8 - ev2boton2.height) / 2;
			addChild(ev2boton2);
			ev2boton2.buttonMode = true;
			ev2boton2.useHandCursor = true;
			ev2boton3.x = stage.stageWidth * 2 / 3 + (stage.stageWidth / 3 - ev2boton3.width) / 2;
			ev2boton3.y = stage.stageHeight * 7 / 8 + (stage.stageHeight/8 - ev2boton3.height) / 2;
			addChild(ev2boton3);
			ev1boton3.buttonMode = true;
			ev1boton3.useHandCursor = true;
			
			ev2boton1.addEventListener(MouseEvent.CLICK, onClick2);
			ev2boton2.addEventListener(MouseEvent.CLICK, onClick2);
			ev2boton3.addEventListener(MouseEvent.CLICK, onClick2);
			contenedor_opciones.visible = true;
			
		}
		private function onClick2(e:MouseEvent):void {
			switch(e.currentTarget) {
				case ev2boton1:
					jugador.vivo = false;
					break;
				case ev2boton2:
					se_mueve = true;
					break;
				case ev2boton3:
					jugador.vivo = false;
					break;
			}
			removeEventListener(MouseEvent.CLICK, onClick2);
			removeChild(ev2boton1);
			removeChild(ev2boton2);
			removeChild(ev2boton3);
			ocurrio_event2 = true;
			en_evento = false;
			contenedor_opciones.visible = false;
			
		}
		
		private function event3():void {
			
			ev3boton1.x = (stage.stageWidth / 3 - ev3boton1.width) / 2;
			ev3boton1.y = stage.stageHeight * 7 / 8 + (stage.stageHeight/8 - ev3boton1.height) / 2;
			addChild(ev3boton1);
			ev3boton1.buttonMode = true;
			ev3boton1.useHandCursor = true;
			ev3boton2.x = stage.stageWidth / 3 + (stage.stageWidth / 3 - ev3boton2.width) / 2;
			ev3boton2.y = stage.stageHeight * 7 / 8 + (stage.stageHeight/8 - ev3boton2.height) / 2;
			addChild(ev3boton2);
			ev3boton2.buttonMode = true;
			ev3boton2.useHandCursor = true;
			ev3boton3.x = stage.stageWidth * 2 / 3 + (stage.stageWidth / 3 - ev3boton3.width) / 2;
			ev3boton3.y = stage.stageHeight * 7 / 8 + (stage.stageHeight/8 - ev3boton3.height) / 2;
			addChild(ev3boton3);
			ev3boton3.buttonMode = true;
			ev3boton3.useHandCursor = true;
			
			ev3boton1.addEventListener(MouseEvent.CLICK, onClick3);
			ev3boton2.addEventListener(MouseEvent.CLICK, onClick3);
			ev3boton3.addEventListener(MouseEvent.CLICK, onClick3);
			contenedor_opciones.visible = true;
			
		}
		private function onClick3(e:MouseEvent):void {
			switch(e.currentTarget) {
				case ev3boton1:
					jugador.vivo = false;
					break;
				case ev3boton2:
					jugador.vivo = false;
					break;
				case ev3boton3:
					se_mueve = true;
					break;
			}
			removeEventListener(MouseEvent.CLICK, onClick3);
			removeChild(ev3boton1);
			removeChild(ev3boton2);
			removeChild(ev3boton3);
			ocurrio_event3 = true;
			en_evento = false;
			contenedor_opciones.visible = false;
		}
		
		private function teleport1():void {
			jugador.x = teleport_sale_1.x;
			jugador.y = teleport_sale_1.y;
		}
	}
}